<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2018-02-26 14:03:35 apple $
 * 错误日志
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['types']=array(
	'1'=>'函数报错',
	'2'=>'exi函数报错',
	'3'=>'数据库错误',
	'4'=>'前端返回错误',
);
$return['thiss']=array(
	'1'=>'管理端',
	'2'=>'客户端',
);
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array();
	if(isset($_RQ['type'])){
		$params['type']=intval($_RQ['type']);
		$return['params']['type']=$_RQ['type'];
	}
	if($_RQ['function']){
		$params['function like']='%'.trim($_RQ['function']).'%';
		$return['params']['function']=$_RQ['function'];
	}
    $fields=array('id','type','function','createtime','message');
    $orderby='id desc';
	$total='';
	$list=pdo_getslice('core_error_log',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if($list){
		foreach($list as &$item){
			if(cfc('tools')->istrlen($item['message'])>30){
				$item['message']=cfc('tools')->substr($item['message'],0,30);
				$item['message'].='...';
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'日志列表'),
			array('op'=>'delall','title'=>'清空日志'),
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'id','title'=>'错误ID','type'=>'string'),
			array('name'=>'type','title'=>'错误类型','type'=>'radio','options'=>'types'),
			array('name'=>'function','title'=>'报错函数','type'=>'string'),
			array('name'=>'createtime','title'=>'报错时间','type'=>'time'),
			array('name'=>'message','title'=>'消息内容','type'=>'string'),
		),'action'=>array(
			array('type'=>'href','op'=>'look','title'=>'详情'),
			array('type'=>'post','op'=>'up','title'=>'上报'),
		)),
		'search_one'=>array('name'=>'title','select_name'=>'type','select_title'=>'请选择错误类型','is_radio'=>true,'options'=>'types','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='look'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择要查看的错误记录！','error');
	}
	$item=pdo_get('core_error_log',array('id'=>$id));
	$item['type']=$return['types'][$item['type']];unset($return['types']);
    $return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'desc'=>'错误详情',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表页'),
		),
		'look'=>array(
			array('name'=>'type','title'=>'错误类型','type'=>'string'),
			array('name'=>'function','title'=>'报错函数','type'=>'string'),
			array('name'=>'createtime','title'=>'报错时间','type'=>'time'),
			array('name'=>'message','title'=>'消息内容','type'=>'string'),
			array('name'=>'session','title'=>'session内容','type'=>'string'),
			array('name'=>'debug','title'=>'debug','type'=>'string'),
			array('name'=>'query','title'=>'请求参数','type'=>'string'),
			array('name'=>'postdata','title'=>'post数据','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='up'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择要上报的错误记录！','error');
	}
	$log=pdo_get('core_error_log',array('id'=>$id));
	if(!$log){
		exi('找不到要上报的错误记录！','error');
	}
	$log['error_id']=$log['id'];
	cloud_request('cloud.log_error',$log);
	exi('上报成功！');
}
if($_RQ['op']=='delall'){
	pdo_delete('core_error_log',array());
	exi('清空日志成功！','','list');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_error_log',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_error_log',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}

?>